package fun.tan90.easy.binlog.worker.handler;

import fun.tan90.easy.binlog.base.constants.EasyBinlogConstants;
import fun.tan90.easy.binlog.base.convention.enums.IErrorCode;
import fun.tan90.easy.binlog.base.convention.exception.ServiceException;
import fun.tan90.easy.binlog.base.strategy.AbstractExecuteStrategy;
import fun.tan90.easy.binlog.plugin.common.service.PluginService;
import lombok.extern.slf4j.Slf4j;
import org.noear.socketd.transport.core.Entity;
import org.noear.socketd.transport.core.Message;
import org.noear.socketd.transport.core.entity.StringEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 19:42
 */
@Slf4j
@Component
public class OutputTestHandler implements AbstractExecuteStrategy<Message, Entity> {

    @Resource
    Map<String, PluginService> pluginServiceMap;

    @Override
    public String mark() {
        return EasyBinlogConstants.OUTPUT_TEST;
    }

    @Override
    public Entity executeResp(Message message) {
        String pluginType = message.meta("pluginType");
        PluginService pluginService = pluginServiceMap.get(pluginType);
        if (Objects.isNull(pluginService)) {
            log.error("【{}】插件不存在", pluginType);
            throw new ServiceException(IErrorCode.BINLOG_1004007);
        }
        return new StringEntity(String.valueOf(pluginService.test(message.dataAsString())));
    }
}
